package com.bluefuture.modules.app.service.impl;
import java.util.*;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.bluefuture.common.exception.GlobalException;
import com.bluefuture.common.utils.ResultEnum;
import com.bluefuture.common.utils.ResultVo;
import com.bluefuture.modules.app.form.OrderCheckForm;
import com.bluefuture.modules.sys.security.user.SecurityUser;
import com.google.common.collect.Lists;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bluefuture.common.utils.PageUtils;
import com.bluefuture.common.utils.Query;

import com.bluefuture.modules.app.dao.AppOrderDao;
import com.bluefuture.modules.app.entity.AppOrderEntity;
import com.bluefuture.modules.app.service.AppOrderService;

import javax.validation.constraints.NotBlank;


@Service("appOrderService")
public class AppOrderServiceImpl extends ServiceImpl<AppOrderDao, AppOrderEntity> implements AppOrderService {
    public static void main(String[] args) {
        System.out.println(new Date(1598955146000L));
    }

    @Autowired
    private IosServiceImpl iosService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AppOrderEntity> page = this.page(
                new Query<AppOrderEntity>().getPage(params),
                new QueryWrapper<AppOrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public String generateOrderNo(Integer type) {
        ArrayList<Integer> types = Lists.newArrayList(1, 2, 3);
        if (!types.contains(type)){
            throw new GlobalException(ResultEnum.PARAM_ERROR);
        }
        AppOrderEntity orderEntity = new AppOrderEntity();
        orderEntity.setUserId(SecurityUser.getUserId().intValue());
        orderEntity.setStatus(0);
        orderEntity.setType(type);
        orderEntity.setCreateTime(new Date());
        orderEntity.setUpdateTime(new Date());
        orderEntity.setOrderNo(UUID.randomUUID().toString());
        this.save(orderEntity);
        return orderEntity.getOrderNo();
    }

    @Override
    public List<AppOrderEntity> aboutToExpireList() {
        LambdaQueryWrapper<AppOrderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(AppOrderEntity::getStatus,3);
        List<AppOrderEntity> list = list(wrapper);
        return list;
    }

    @Override
    public List<AppOrderEntity> waitForCheckList() {
        LambdaQueryWrapper<AppOrderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppOrderEntity::getStatus,1);
        List<AppOrderEntity> list = list(wrapper);
        return list;
    }

    @Override
    public boolean orderCheck(OrderCheckForm form) {
        AppOrderEntity appOrderEntity = updateOrder(form);
        if (appOrderEntity == null){
            log.error("订单记录为空");
            return false;
        }
        ResultVo resultVo = iosService.iosInAppPurchase(appOrderEntity);
        return resultVo.getCode()==0;

    }

    private AppOrderEntity updateOrder(OrderCheckForm form){
        LambdaQueryWrapper<AppOrderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppOrderEntity::getOrderNo,form.getOrderNo())
                .eq(AppOrderEntity::getUserId,SecurityUser.getUserId());
        List<AppOrderEntity> list = list(wrapper);
        if (CollectionUtils.isNotEmpty(list)){
            AppOrderEntity appOrderEntity = list.get(0);
            appOrderEntity.setSeq(form.getReceipt());
            appOrderEntity.setUpdateTime(new Date());
            updateById(appOrderEntity);
            return appOrderEntity;
        }
        return null;
    }
}